/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------------------------------------*/
/* Includes of system headers                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "MassStorage_ISP.h"
#include "Driver\DrvFMC.h"
#include "pronano.h"


/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions                                                                    */
/*---------------------------------------------------------------------------------------------------------*/

#define FLASH_PAGE_SIZE         512
//#define EUR_CSV 1

extern uint32_t g_u32FlashSize;
extern uint8_t g_u8SecurityLockBit;
extern volatile uint8_t g_u8IsPrepareFAT;

uint8_t u8FormatData[62] = 
{
    0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53,
    0x35, 0x2E, 0x30, 0x00, 0x02, 0x01, 0x06, 0x00,
    0x02, 0x00, 0x02, 0xA8, 0x00, 0xF8, 0x01, 0x00,
    0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0xB9,
    0xC1, 0xAA, 0x42, 0x4E, 0x4F, 0x20, 0x4E, 0x41,
    0x4D, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41,
    0x54, 0x31, 0x32, 0x20, 0x20, 0x20
};

/*unsigned char u8DirData[128] = {
   'm', 'i', 'c', 'h', 'a', 'e', 'l', 'T',
   ' ', ' ', ' ', 0x28, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x52, 0x45, 0x41, 0x44, 0x4D, 0x45, 0x20, 0x20, 0x54, 0x58, 0x54, 0x22, 0x18, 0x25, 0x57, 0x72, 
	0xFD, 0x3E, 0xFD, 0x3E, 0x00, 0x00, 0x58, 0x72, 0xFD, 0x3E, 0x02, 0x00, 0x21, 0x00, 0x00, 0x00, 
	0x4B, 0x4E, 0x49, 0x47, 0x48, 0x54, 0x20, 0x20, 0x54, 0x58, 0x54, 0x22, 0x18, 0x28, 0xD2, 0x4C, 
	0x01, 0x3F, 0x01, 0x3F, 0x00, 0x00, 0xCE, 0x4C, 0x01, 0x3F, 0x03, 0x00, 0x21, 0x04, 0x00, 0x00
};*/
/*unsigned char Def_Dir_Entry[32] = {
	0x4B, 0x4E, 0x49, 0x47, 0x48, 0x54, 0x20, 0x20, 0x54, 0x58, 0x54, 0x22, 0x18, 0x28, 0xD2, 0x4C, 
	0x01, 0x3F, 0x01, 0x3F, 0x00, 0x00, 0xCE, 0x4C, 0x01, 0x3F, 0x03, 0x00, 0x21, 0x04, 0x00, 0x00
};*/
unsigned char Def_Dir_Entry[32] = {
	0x4B, 0x4E, 0x49, 0x47, 0x48, 0x54, 0x20, 0x20, 0x54, 0x58, 0x54, 0x20, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x21, 0x04, 0x00, 0x00
};
unsigned char u8DirData[(MAX_Sheets+1)*32] = {
#ifdef LOCCUS_ODM
	'L', 'o', 'c', 'c', 'u', 's', 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x00, 0x00, 0x00, 0x00, 
#elif defined TIANGEN_ODM
	'T', 'G', 'e', 'm', 'S', 'p', 'e', 'c', 0x20, 0x20, 0x20, 0x28, 0x00, 0x00, 0x00, 0x00, 
#elif defined KASVI_ODM
    'K', 'A', 'S', 'V', 'I', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x00, 0x00, 0x00, 0x00, 
#else
	'M', 'G', '_', 'N', 'A', 'N', 'O', 0x20, 0x20, 0x20, 0x20, 0x28, 0x00, 0x00, 0x00, 0x00, 
#endif
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x52, 0x45, 0x41, 0x44, 0x4D, 0x45, 0x20, 0x20, 0x54, 0x58, 0x54, 0x22, 0x18, 0x25, 0x57, 0x72, 
	0xFD, 0x3E, 0xFD, 0x3E, 0x00, 0x00, 0x58, 0x72, 0xFD, 0x3E, 0x02, 0x00, 0x21, 0x00, 0x00, 0x00, 
	0x4B, 0x4E, 0x49, 0x47, 0x48, 0x54, 0x20, 0x20, 0x54, 0x58, 0x54, 0x22, 0x18, 0x28, 0xD2, 0x4C, 
	0x01, 0x3F, 0x01, 0x3F, 0x00, 0x00, 0xCE, 0x4C, 0x01, 0x3F, 0x03, 0x00, 0x21, 0x04, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//unsigned char u8FATData[256];
unsigned char u8FATData[256] = {
	0xF8, 0xFF, 0xFF, 0xFF, 0x4F, 0x00, 0x05, 0xF0, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
extern struct Measure_sheet sheet_info[MAX_Sheets];
short FAT_cluster_start_end[MAX_Sheets];
uint32_t total_sheets = 0;
int circular_index[MAX_Sheets] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
unsigned char u8SheetData[2048];

void Prepare_FAT(void) {
	uint32_t i, j, u8FATData_index, nth_cluster;
	struct dir_entry dir_entry_buf;
    struct Measure_tran temp_mea1;
	union MaestroDate *pDate;

//#ifdef 0
//prepare directory entry & FAT table
	total_sheets = 0;
        if (head_sheet < tail_sheet) {
          for (i = 0; i < MAX_Sheets; i++)
	         if (i < tail_sheet) {
	           circular_index[i] = tail_sheet - i;
		       total_sheets++;
             }
	         else {
                circular_index[i] = -1;
             }
        }
        else 
           if (head_sheet > tail_sheet) {
             /*for (j = 0, i = tail_sheet - 1; i >= 0; i--)
	            circular_index[j++] = i + 1;
             for (i = MAX_Sheets-1; i >= tail_sheet; i--)
                circular_index[j++] = i+1;
	         total_sheets = MAX_Sheets;*/
			 for (i = 0; i <  MAX_Sheets; i++) {
			     circular_index[i] = i+1;
			 }
			 //circular_index[MAX_Sheets-1] = -1;
			 total_sheets = 10;
           }
        my_memcpy((uint8_t *)&dir_entry_buf, Def_Dir_Entry, 32);
        my_memcpy((uint8_t *)dir_entry_buf.Extension, "CSV", 3);
        dir_entry_buf.FstClusLO = 2;
//fill FAT cluster link list
        memset((u8FATData), 0x0, sizeof(u8FATData));
        u8FATData[0] = 0xF8;
        u8FATData[1] = u8FATData[2] = 0xFF;
        u8FATData_index = 3;
        nth_cluster = 0;

        for (i = 0 ; i < MAX_Sheets; i++) {
           printf("%d ", circular_index[i]);
           if (circular_index[i] > 0) {
             sprintf(buf2, "SHEET_%02d", circular_index[i]);
             memset(dir_entry_buf.Filename, 0x20, sizeof(dir_entry_buf.Filename));
             my_memcpy((uint8_t *)dir_entry_buf.Filename, buf2, strlen(buf2));
//calculate real export raw data size
             dir_entry_buf.FileSize = 0;
             dir_entry_buf.FileSize += strlen("===============dsDNA===============\n\r");
             dir_entry_buf.FileSize += strlen(" Date xxxx/yy/zz\n\r");
             dir_entry_buf.FileSize += strlen(" Time xx:yy:zz\n\r");
             dir_entry_buf.FileSize += strlen("===================================\n\r");
             Load_sheet(&temp_mea1, &sheet_info[circular_index[i]-1], circular_index[i]-1);
			 pDate = &sheet_info[circular_index[i]-1].sheet_date;;  //A.D. 1980 PC is created
			 dir_entry_buf.WrtDate = 0;
			 dir_entry_buf.WrtDate = pDate->PackDate.Day;
             dir_entry_buf.WrtDate += pDate->PackDate.Month << 5;
			 dir_entry_buf.WrtDate += (pDate->PackDate.Year - 1980) << 9;
			 dir_entry_buf.CrtDate = dir_entry_buf.WrtDate;
			 dir_entry_buf.WrtTime = 0;
			 dir_entry_buf.WrtTime = pDate->PackDate.Second / 2;
			 dir_entry_buf.WrtTime += pDate->PackDate.Minute << 5;
			 dir_entry_buf.WrtTime += pDate->PackDate.Hour << 11;
			 dir_entry_buf.CrtTime = dir_entry_buf.WrtTime;
             if (sheet_info[circular_index[i]-1].sheet_type < 4) {
			   dir_entry_buf.FileSize += strlen("Sample_index,AXXX\n\r");
               for (j = 0; j < (temp_mea1.sample_index+temp_mea1.blank_flag); j++) {
                  sprintf(buf2, "%d,xxxx.xxx\n\r");
                  dir_entry_buf.FileSize += strlen(buf2);
               }
             }
             else {
                /*dir_entry_buf.FileSize += strlen("Sample_index,A280\n\r");
                for (j = 0; j < (temp_mea1.sample_index+temp_mea1.blank_flag); j++) {
                   sprintf(buf2, "%d,xx.xxx\n\r");
                   dir_entry_buf.FileSize += strlen(buf2);
                }*/
             }
             //dir_entry_buf.FileSize = 0x47;
             my_memcpy((uint8_t *)(u8DirData+32*(i+1)), (uint8_t *)&dir_entry_buf, 32);
			 //memset((uint8_t *)(u8DirData+32*(i+1)), 0, 32);
			 printf("dir_entry_buf.FileSize: %d, dir_entry_buf.FstClusLO:%d, dir_entry_buf size: %d, %d\n", dir_entry_buf.FileSize, dir_entry_buf.FstClusLO, sizeof(dir_entry_buf), sizeof(long));
             FAT_cluster_start_end[i] = dir_entry_buf.FstClusLO;
	         FAT_cluster_start_end[i] <<= 8;
             printf("start cluster: %d\n", FAT_cluster_start_end[i]);
			 if (dir_entry_buf.FileSize%512)
	           dir_entry_buf.FstClusLO += dir_entry_buf.FileSize/512 + 1;
	         else
	            dir_entry_buf.FstClusLO += dir_entry_buf.FileSize/512;
//#ifdef 0
			 FAT_cluster_start_end[i] |= dir_entry_buf.FstClusLO;
	         for (j = ((FAT_cluster_start_end[i] >> 8) + 1); j < (dir_entry_buf.FstClusLO); j++) {
                printf("end cluster: %d\n", j);
				if (!(nth_cluster%2)) {
	              u8FATData[u8FATData_index++] = j & 0x00ff;
	      	      u8FATData[u8FATData_index] |= (j >> 8) & 0x000f;
	      	      nth_cluster++;
	            }
	            else {
	      	       u8FATData[u8FATData_index++] |= (j & 0x000f) << 4;
	      	       u8FATData[u8FATData_index++] |= (j >> 4) & 0x00ff;
	      	       nth_cluster++;
	            }
	         }
	         if (!(nth_cluster%2)) {
	      	   u8FATData[u8FATData_index++] = 0xff;
	      	   u8FATData[u8FATData_index] |= 0x0f;
	      	   nth_cluster++;
	         }
	         else {
	      	    u8FATData[u8FATData_index++] |= 0x0f << 4;
	      	    u8FATData[u8FATData_index++] |= 0xff;
	      	    nth_cluster++;
	         }
//#endif
           }
           else {
              memset((u8DirData+32*(i+1)), 0x0, 32);
			  FAT_cluster_start_end[i] = 0;
           }
        }
        printf("\n ", circular_index[i]);
//#endif
  g_u8IsPrepareFAT = 1;
}

void myFMC_Read(uint32_t u32addr, uint32_t *p_u32data)
{
    /*outpw(FMC_ISPCMD, 0x00);
    outpw(FMC_ISPADR, u32addr);
    outpw(FMC_ISPTRG, 0x01);
    __ISB();
	while (inpw(FMC_ISPTRG));
	*p_u32data = inpw(FMC_ISPDAT);*/
  UNLOCKREG();
  DrvFMC_EnableISP();
  DrvFMC_Read(u32addr, p_u32data);
  LOCKREG();
}

void DataFlashRead(uint32_t addr, uint32_t size, uint32_t buffer)
{
    /* This is low level read function of USB Mass Storage */
    uint32_t i, u32startAddr, j;
	uint32_t * u32buff;
	struct Measure_tran temp_mea1, *pMea_tran;
	struct Measure_sheet *pSheet;
	int32_t read_sec, nth_sec;
    char *str = NULL;

	u32startAddr = addr;
	u32buff = (uint32_t *) buffer;
    printf("Read LBA: %08x\n", u32startAddr);
    for (i = 0; i < FLASH_PAGE_SIZE/4; i++)
    {
        u32buff[i] = 0;
    }
    if (u32startAddr == 0x00000000)
    {
        my_memcpy((uint8_t *)u32buff, u8FormatData, 62);
        
        u32buff[FLASH_PAGE_SIZE/4-1] = 0xAA550000;            
    }
    else
    {
		if ( (u32startAddr == (FAT_SECTORS * 512)) || (u32startAddr == ((FAT_SECTORS+1) * 512)) )
        {
            /*u32buff[0] = 0xFFFFFFF8;
			u32buff[1] = 0xF005004F;
			u32buff[2] = 0x000000FF;*/
			//u32buff[2] = 0x000FFFFF;
            my_memcpy((uint8_t *)u32buff, u8FATData, 256);
        }
		else
		   if (u32startAddr==ROOT_DIR_ADDRESS) {
		     my_memcpy((uint8_t *)u32buff, u8DirData, (MAX_Sheets+1)*32);
			 //my_memcpy((uint8_t *)u32buff, u8DirData, 128);
		   }
		   else
		      if ( (u32startAddr >= DATA_SECTOR_ADDRESS) && (u32startAddr < (DATA_SECTOR_ADDRESS + g_u32FlashSize)) ) {
			  	//my_memcpy((uint8_t *)u32buff, u8FileContent, 29);
                u32startAddr -= DATA_SECTOR_ADDRESS;
				printf("read flash base addr: %08x\n", u32startAddr);
//				if (u32startAddr/FLASH_PAGE_SIZE
                read_sec = u32startAddr/FLASH_PAGE_SIZE + 2;
				pMea_tran = &temp_mea1;
				printf("total sheet: %d\n", total_sheets);
			    for (i = 0, nth_sec = -1; i < total_sheets; i++) {
					if (read_sec == (FAT_cluster_start_end[i] >> 8)) {
					 memset(u8SheetData, 0, sizeof(u8SheetData));
					 Load_sheet(&temp_mea1, &sheet_info[circular_index[i]-1], circular_index[i]-1);
					 pSheet = &sheet_info[circular_index[i]-1];
                     switch(pSheet->sheet_type) {
	                   case 1:
                          strcpy(u8SheetData, "================BCA================\r\n");
                          break;
	                   case 2:
                          strcpy(u8SheetData, "==============Bradford=============\r\n");
                          break;
	                   case 3:
                          strcpy(u8SheetData, "===============Pierce==============\r\n");
                          break;
                     }
                     sprintf(u8SheetData+strlen(u8SheetData), " Date %04d/%02d/%02d\r\n", pSheet->sheet_date.PackDate.Year, pSheet->sheet_date.PackDate.Month, pSheet->sheet_date.PackDate.Day);
                     sprintf(u8SheetData+strlen(u8SheetData), " Time %02d:%02d:%02d\r\n", pSheet->sheet_date.PackDate.Hour, pSheet->sheet_date.PackDate.Minute, pSheet->sheet_date.PackDate.Second);
                     strcat(u8SheetData+strlen(u8SheetData), "===================================\r\n");
#ifdef EUR_CSV
                     if (sheet_info[circular_index[i]-1].sheet_type < 4) {
						 switch (pSheet->sheet_type) {
                           case 1:
							   strcat(u8SheetData+strlen(u8SheetData), "Sample_index;A562\r\n");
							   break;
						   case 2:
							   strcat(u8SheetData+strlen(u8SheetData), "Sample_index;A595\r\n");
							   break;
						   case 3:
							   strcat(u8SheetData+strlen(u8SheetData), "Sample_index;A660\r\n");
							   break;
						 }
					   
					   str = u8SheetData + strlen(u8SheetData);
                       for (j = 0; j < (temp_mea1.sample_index+temp_mea1.blank_flag); j++)
                          sprintf(u8SheetData+strlen(u8SheetData), "%d;%2.3f\r\n", (j+1), pMea_tran->AXXX[j]);
					   while ((str = strchr(str, '.')) != NULL) {
						 *str = ',';
						 str++;
					   }
                     }
#else
                     if (sheet_info[circular_index[i]-1].sheet_type < 4) {
						 switch (pSheet->sheet_type) {
	                       case 1:
		                       strcat(u8SheetData+strlen(u8SheetData), "Sample_index;A562\r\n");
							   for (j = 0; j < (temp_mea1.sample_index+temp_mea1.blank_flag); j++) {
							      sprintf(u8SheetData+strlen(u8SheetData), "%d,%3.3f\r\n", (j+1), pMea_tran->AXXX[j]*1000);
						       }
		                       break;
						   case 2:
							   strcat(u8SheetData+strlen(u8SheetData), "Sample_index;A595\r\n");
							   for (j = 0; j < (temp_mea1.sample_index+temp_mea1.blank_flag); j++) {
							      sprintf(u8SheetData+strlen(u8SheetData), "%d,%3.3f\r\n", (j+1), pMea_tran->AXXX[j]*1000);
						       }
							   break;
						   case 3:
							   strcat(u8SheetData+strlen(u8SheetData), "Sample_index;A660\r\n");
							   for (j = 0; j < (temp_mea1.sample_index+temp_mea1.blank_flag); j++) {
							      sprintf(u8SheetData+strlen(u8SheetData), "%d,%4.3f\r\n", (j+1), pMea_tran->AXXX[j]*10000);
						       }
							   break;
						 }
						 /*for (j = 0; j < (temp_mea1.sample_index+temp_mea1.blank_flag); j++) {
							 sprintf(u8SheetData+strlen(u8SheetData), "%d,%2.3f\r\n", (j+1), pMea_tran->AXXX[j]);
						 }*/
                     }
#endif
                     else {
     
                     }
                     nth_sec = 0;
				   }
				   else 
				      if (read_sec < (FAT_cluster_start_end[i] & 0x00ff))
				      {
				        nth_sec = read_sec - (FAT_cluster_start_end[i] >> 8);
				      }
					  printf("read_sec: %d, nth_sec: %d\n", read_sec, nth_sec);
				   if (nth_sec >= 0) {
				     memcpy(u32buff, u8SheetData+512*nth_sec, 512);
					 break;
				   }
				}
                /*for (i = 0; i < FLASH_PAGE_SIZE/4; i++)
                {
                    myFMC_Read(u32startAddr + i*4, &u32buff[i]);
                }*/
			  }
    }
}


void DataFlashWrite(uint32_t addr, uint32_t size, uint32_t buffer)
{
    /* This is low level write function of USB Mass Storage */
}